#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os, re, string

reload(sys)
sys.setdefaultencoding('utf8')

class info:
    def version(self):
        version_file    = open('/proc/version')
        rtn             = re.compile('Linux version (\S+\d+)').match(version_file.read()).group(1)
        version_file.close()
        return rtn

    def cpu(self):
        cpu_file = open('/proc/stat')
        self.cpu2 = map(string.atof, re.compile('\d+').findall(cpu_file.readline()))
        rtn = (1-((self.cpu2[3]-self.cpu1[3])/(sum(self.cpu2)-sum(self.cpu1))))*100
        cpu_file.close()
        self.cpu1 = self.cpu2
        return rtn

    def mem(self):
        mem_file = open('/proc/meminfo')
        mem_text = mem_file.read()
        total   = string.atof(re.compile('MemTotal: *(\S+)').search(mem_text).group(1))
        free    = string.atof(re.compile('MemFree: *(\S+)').search(mem_text).group(1))
        buffers = string.atof(re.compile('Buffers: *(\S+)').search(mem_text).group(1))
        cached  = string.atof(re.compile('Cached: *(\S+)').search(mem_text).group(1))
        mem_file.close()
        return (total-free-buffers-cached)*100/total

    def net(self):
        speed2 = {'down':0, 'up':0}
        rtn = ['','']
        net_file = open('/proc/net/dev')
        for line in net_file.readlines()[2:]:
            tmp = map(string.atof, re.compile('\d+').findall(line[line.find(':'):]))
            speed2['down']  += tmp[0]
            speed2['up']    += tmp[8]
        net_file.close()
        down    = speed2['down']-self.speed1['down'] if self.speed1['down'] else 0
        up      = speed2['up']-self.speed1['up'] if self.speed1['up'] else 0

        if down >= 1000 and down <= 1000*1000:
            rtn[0] = '%5.1f%s' % (down/1024,'KB')
        elif down >= 1000*1000 and up <= 1000*1000*1000:
            rtn[0] = '%5.1f%s' % (down/(1024*1024),'MB')
        elif down >= 1000*1000*1000:
            rtn[0] = '%5.1f%s' % (down/(1024*1024*1024),'GB')
        else:
            rtn[0] = '%5.1f%s' % (down,' B')

        if up >= 1000 and up <= 1000*1000:
            rtn[1] = '%5.1f%s' % (up/1024,'KB')
        elif up >= 1000*1000 and up <= 1000*1000*1000:
            rtn[1] = '%5.1f%s' % (up/(1024*1024),'MB')
        elif up >= 1000*1000*1000:
            rtn[0] = '%5.1f%s' % (up/(1024*1024*1024),'GB')
        else:
            rtn[1] = '%5.1f%s' % (up,' B')
        self.speed1 = speed2
        return rtn

#    def battery(self):
#        battery_full_file   = open('/proc/acpi/battery/BAT0/info')
#        battery_remain_file = open('/proc/acpi/battery/BAT0/state')
#        tmp = battery_full_file.read()
#
#        battery_design      = string.atof(re.compile('design capacity:\s+(\d+)').search(tmp).group(1))
#        battery_last_full   = string.atof(re.compile('last full capacity:\s+(\d+)').search(tmp).group(1))
#        battery_remain      = string.atof(re.compile('remaining capacity:\s+(\d+)').search(battery_remain_file.read()).group(1))
#
#        battery_full_file.close()
#        battery_remain_file.close()
#        return battery_remain*100/(battery_design if battery_remain == battery_design else battery_last_full)


    def __init__(self):
        self.cpu1   = [0, 0, 0, 0, 0, 0, 0, 0]
        self.speed1 = {'down':0, 'up':0}
